package com.ja.openchat.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonObject;
import com.ja.openchat.BuildConfig;
import com.ja.openchat.R;
import com.ja.openchat.ui.update.UpdateInfo;
import com.ja.openchat.ui.update.UpdateUtil;
import com.zy.openchat.App;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.version.AppVersionInfo;

import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;


public class AboutViewModel extends BaseViewModel {

    public ObservableField<String> version = new ObservableField<>(App.getInstanse().getString(R.string.version_text) + BuildConfig.VERSION_NAME + "_" + BuildConfig.VERSION_CODE);

    public AboutViewModel(@NonNull Application application) {
        super(application);
    }

    public SingleLiveEvent<UpdateInfo> updateInfoSingleLiveEvent = new SingleLiveEvent<>();
    //点击事件
    public BindingCommand btnOnClickCommand = new BindingCommand(() -> {

    });
    public BindingCommand btnOnClickTousu = new BindingCommand(() -> {
        ToastUtils.showShort(getApplication().getString(R.string.function_not_open));
//        Bundle bundle = new Bundle();
//        bundle.putString("url", Constants.KHostEReport);
//        bundle.putString("title", getApplication().getString(R.string.feedback_text));
//        startActivity(PreviewWebViewActivity.class, bundle);
    });
    //官网点击事件
    public BindingCommand officeOnClickCommand = new BindingCommand(() -> {
        ToastUtils.showShort(getApplication().getString(R.string.function_not_open));
//        Bundle bundle = new Bundle();
//        bundle.putString("url", Constants.KHostAddress);
//        bundle.putString("title", getApplication().getString(R.string.official_website));
//        startActivity(PreviewWebViewActivity.class, bundle);
    });
    //功能介绍点击事件
    public BindingCommand functionOnClickCommand = new BindingCommand(() -> {
        ToastUtils.showShort(getApplication().getString(R.string.function_not_open));
//        Bundle bundle = new Bundle();
//        bundle.putString("url", Constants.KHostAddress);
//        bundle.putString("title", getApplication().getString(R.string.features_text));
//        startActivity(PreviewWebViewActivity.class, bundle);
    });
    //检测新版本点击事件
    public BindingCommand checkVersionOnClickCommand = new BindingCommand(() -> {
        checkUpdate();
    });

    public void checkUpdate() {
        showProgress();
        TelegramManager.getTelegramManager().getAppVersion(getApplication(), UpdateUtil.getVersionCode(getApplication()), new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                hideProgress();
                if (obj.ok && obj.data != null && obj.data instanceof AppVersionInfo) {
                    AppVersionInfo versionInfo = (AppVersionInfo) obj.data;
                    UpdateInfo updateInfo = new UpdateInfo();
                    updateInfo.hasUpdate = true;
                    updateInfo.url = versionInfo.downloadUrl;
                    updateInfo.versionCode = versionInfo.versionCode;
                    updateInfo.versionName = versionInfo.version;
                    updateInfoSingleLiveEvent.setValue(updateInfo);
                } else if (obj.errCode == 201) {
                    showWarnTip(getApplication().getString(R.string.no_new_version));
                } else {
                    showErrorTip(getApplication().getString(R.string.request_error));
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                hideProgress();
                showErrorTip(getApplication().getString(R.string.request_error));
            }
        });
    }
}
